#!/bin/bash
shell_home=$(
  cd "$(dirname "$0")"
  pwd
)
app=go-test
dist=dist
app_id=con.gotest.app
version=0.0.1
build=520
dist_home=$shell_home/$dist
tmp_home=$shell_home/tmp
developerz_id_installer_home=${shell_home}/darwinApp/cert
developerz_id_installer_cer=developerID_installer.cer
icon="Icon.png"
goproxy="https://goproxy.cn,direct"

function create_dir() {
  if [ ! -d $1 ]; then
    echo "Create dir: $1"
    mkdir -p $1
  fi
}

function build() {
  echo "fyne build by fyne-corss for darwin"
  fyne-cross darwin -app-id=${app_id} -app-version=${version} -app-build=${build} -arch=amd64 -icon=${icon} --env GOPROXY=${goproxy}
  arch_path=darwin-amd64
  fyne_dist_file="${shell_home}/fyne-cross/dist/${arch_path}/${app}.app"
  tmp_files_home="${tmp_home}/${arch_path}"
  create_dir ${tmp_files_home}
  create_dir ${dist_home}/${version}
  cp -rf ${fyne_dist_file} ${tmp_files_home}

  /usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier string applet url" ${tmp_files_home}/${app}.app/Contents/Info.plist
  /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes array" ${tmp_files_home}/${app}.app/Contents/Info.plist
  /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0 dict" ${tmp_files_home}/${app}.app/Contents/Info.plist
  /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleTypeRole string Shell" ${tmp_files_home}/${app}.app/Contents/Info.plist
  /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLName string applet url" ${tmp_files_home}/${app}.app/Contents/Info.plist
  /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes array" ${tmp_files_home}/${app}.app/Contents/Info.plist
  /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes:0 string t1-sslvpn-client" ${tmp_files_home}/${app}.app/Contents/Info.plist

  pkgbuild --root ${tmp_files_home} --identifier ${app_id} --version ${version} --install-location /Applications --keychain ${developerz_id_installer_home}/${developerz_id_installer_cer} ${dist_home}/${version}/${arch_path}/${app}.pkg

  # cp -rf ${tmp_files_home} ${dist_home}/${version}/
  # ${dist_home}/${version}/${oem}

}
build